﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class AdminForShopApprove : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdVirtualShop;
                SqlDataReader dtrVirtualShop;
                conn.Open();

                cmdVirtualShop = new SqlCommand("SELECT * FROM Users u, Virtual_Shop v WHERE u.u_id=v.u_id AND u.u_position='ShopOwner' AND v.v_status='Activate'", conn);
                dtrVirtualShop = cmdVirtualShop.ExecuteReader();

                ItemList.DataSource = dtrVirtualShop;
                ItemList.DataBind();

                dtrVirtualShop.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {
                MessageBox(ex.Message);
            }
        }

        protected void ItemList_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
        {
            int pid;//virtual shop ID

            if (e.CommandName == "ViewShop")
            {
                pid = Convert.ToInt32(e.CommandArgument.ToString());

                Response.Redirect("VirtualShop.aspx?id=" +pid);
            }

            else if (e.CommandName == "DeleteShop")
            {
                pid = Convert.ToInt32(e.CommandArgument.ToString());
               
                SqlCommand dltVirtualShop;
                conn.Open();

                dltVirtualShop = new SqlCommand("UPDATE Virtual_Shop SET v_status='Deactivate' WHERE v_id=@id", conn);
                dltVirtualShop.Parameters.AddWithValue("@id", pid);
                dltVirtualShop.ExecuteNonQuery();

                MessageBox("Delete successfully!");
                Redirect("AdminForVirtualShopManage.aspx");
            }
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

    }
}